import { logger } from "@/utils/logger";
import { videoAdUnitId } from "./config";

const AD_OPTIONS = {
  config: `https://mulit-1257020227.cos.ap-beijing.myqcloud.com/ad-miniprogram.json`,
  bannerStyle: { left: 0, top: 0, width: 300 }, // banner 广告样式
  interstitialDelay: 4000, // 插屏广告延迟加载时间
  errMsg: {
    VIDEO_NOT_DEFINED: `功能尚未开放`, // 广告未接入
    VIDEO_AD_RETRYING: `广告正在加载,请稍后重试`, // 加载失败,正在重试
    VIDEO_AD_LOAD_ERROR: `暂无合适广告(请稍后重试)`, // 重试了好多次,还是不行
    VIDEO_AD_SHOW_ERROR: `广告播放出错,请稍后重试`, // 广告播放出错
    VIDEO_AD_NOT_DEFIND: `暂无合适广告,再试试吧!` // 广告加载成功了,但是现实失败了
  }
};
const api = {
  /**
   * @description 微信自带对话框
   * @author Halo
   * @date 2019-12-25
   * @param {*} options
   * @returns
   */
  showModal(options: WechatMiniprogram.ShowModalOption | any) {
    return new Promise((resolve, reject) => {
      wx.showModal({
        title: `消息`,
        content: ``,
        showCancel: false,
        confirmText: "确定",
        confirmColor: "#3CC51F",
        ...options,
        success: () => resolve(),
        fail: () => reject()
      });
    });
  },
  showToast(title: string, options?: WechatMiniprogram.ShowToastOption | any) {
    return new Promise(resolve => {
      wx.showToast({
        title,
        icon: "none",
        mask: true,
        duration: 3000,
        complete: () => resolve(),
        ...options
      });
    });
  }
};

export const AD: {
  video: string;
  enable: boolean;
  invoke: { video: (cb: (isEnded: boolean) => void) => Promise<any> };
} = {
  video: `adunit-66c8bc564045ebcf`,
  get enable(): boolean {
    return !!AD.video;
  },
  invoke: {
    async video(cb: (isEnded: boolean) => void): Promise<any> {
      const _videoAdUnitId = wx.getStorageSync("jili_aid");
      AD.video = "adunit-482f3fab18029ac0" || _videoAdUnitId; //从线上JSON中获取广告id
      let adUnitId = AD.video;
      if (!adUnitId) return api.showModal({ content: AD_OPTIONS.errMsg.VIDEO_NOT_DEFINED });
      let ad: WechatMiniprogram.RewardedVideoAd = null;
      if (!wx.createRewardedVideoAd) return api.showModal({ content: `抱歉,当前功能仅支持抖音小程序。` });
      ad = wx.createRewardedVideoAd({ adUnitId, multiton: true });
      ad.onError(res => console.log(`errMsg:`, res));
      let close = (res: { isEnded: boolean }) => {
        ad.destroy()
        cb(res.isEnded);
      };
      ad.onClose(close);
      await api.showToast(`正在加载`, { duration: 5000 });
      ad.load().then(() => {
        ad.show()
      })
    }
  }
};

export const interstitialAd: {
  adId: string;
  enable: boolean;
  show: Function;
} = {
  adId: wx.getStorageSync("chaping_aid"),
  enable: true,
  show() {
    let loginStamp = wx.getStorageSync("loginStamp");
    let nowStamp = new Date().getTime();
    if (nowStamp - 15 * 1000 > loginStamp && wx.createInterstitialAd) {
      let AD = wx.createInterstitialAd({ adUnitId: interstitialAd.adId });
      AD.onLoad(res => {
        AD.show();
      });
      AD.onError(res => {
        logger.error(res);
      });
      AD.onClose(res => {
        interstitialAd.enable = false;
        AD.destroy();
      });
    }
  }
};



